<template>
  <div>
    <el-dialog title="添加部门" v-model="dialogFormSysDeptEditVisible">
      <el-form :model="formObj" name="el-form-sys-role-edit-name" id="el-form-sys-role-edit-id"  size="mini">
        <el-form-item label="部门名称" :label-width="formLabelWidth">
          <el-input v-model="formObj.name" name="roleName" autocomplete="off"></el-input>
        </el-form-item>


        <el-form-item label="上级组织" :label-width="formLabelWidth">
          <el-input v-model="formObj.pname" name="pname" v-on:click.native="_pnameClick" :readonly="true" autocomplete="off"></el-input>
        </el-form-item>


        <el-form-item label="排序" :label-width="formLabelWidth">
          <el-input v-model="formObj.seq" name="seq" autocomplete="off"></el-input>
        </el-form-item>



        <el-form-item label="备注" :label-width="formLabelWidth">
          <el-input v-model="formObj.remark" name="remark" autocomplete="off"></el-input>
        </el-form-item>

      </el-form>
      <template #footer>
          <span class="dialog-footer">
            <el-button @click="dialogFormSysDeptEditVisible = false">取 消</el-button>
            <el-button type="primary" @click="_ok">确 定</el-button>
          </span>
      </template>

    </el-dialog>


    <SysDeptSelect ref="sysDeptSelectModel" @on-result-change="_selectBack"></SysDeptSelect>
  </div>

</template>

<script>

  import  SysDeptSelect from './SysDeptSelect'
  import {clearObject, getReq, postReq} from "../../../utils/api";
  import {ElMessage} from "element-plus";
    export default {
      components:{
        SysDeptSelect
      },
      mounted(){
      },

        name: "SysMenuEdit",
      data(){
          return{
            formObj:{
              name:undefined,
              pname:undefined,
              seq:undefined,
              remark:undefined,
              pid:undefined
            },

            pNameList:[],
            parentName: '',

            dialogFormSysDeptEditVisible: false,

            formLabelWidth: '120px'
          }
      },
      methods:{

          open(id){
            if (undefined===id){
              //新增
              clearObject(this.formObj)
              this.dialogFormSysDeptEditVisible=true
            }else{
              this.dialogFormSysDeptEditVisible=true
              getReq('/api/sys_dept/'+id).then(res=>{
                this.formObj=res.data.data
              })
            }

          },

        _pNameChange(data){
            this.form.pid=data;
        },
        _selectBack(data){
           this.formObj.pname=data.name;
           this.formObj.pid = data.id;
        },

        _pnameClick(){
          this.$refs.sysDeptSelectModel.open();
        },

          _ok(){
            let userData= this.formObj
            if (undefined === userData.id){
              postReq('/api/sys_dept/add',userData).then(res=>{
                console.log('res---->',res)
                if (res.data.code===1){
                  this.dialogFormSysDeptEditVisible=false
                  ElMessage.success({
                    message: '添加成功！',
                    type: 'success'
                  });
                  this.$emit('page')
                }else{
                  this.$message({
                    type: 'success',
                    message: '添加成功!'
                  });
                  this.$emit('page')
                  this.dialogFormSysDeptEditVisible = false;
                  this.$emit('on-result-change')
                }

              }).catch(err=>{
                this.$message({
                  type: 'info',
                  message: '添加失败'
                });
              })

            } else {
              this.$http.put('/sys_resource/'+userData.id,userData).then(res=>{
                this.$message({
                  type: 'success',
                  message: '编辑成功!'
                });
                this.$emit('on-result-change')
                this.dialogFormSysDeptEditVisible = false;

              }).catch(err=>{
                this.$message({
                  type: 'info',
                  message: '编辑失败'
                });
              })
            }




          }

      },
    }
</script>

<style scoped>

</style>
